const inquirer = require("inquirer");
const { boolChoies, inquirerConfig } = require("../config");
const t = require("../config/dict");
const setProjectConfigFn = require("./setProjectConfig");
const { log } = require("../lib/utils");

module.exports = (context) => {
  const projectName = context.name;
  console.log("");
  log("tip", {
    message: [
      "1)",
      t(context.lang, "设置项目"),
      projectName,
      t(context.lang, "配置"),
      t(context.lang, "。"),
    ].join(" "),
  });

  const setProjectConfig = (context) => {
    return setProjectConfigFn(context);
  };

  const done = (context) => {
    return inquirer.prompt([...inquirerConfig(context)]).then((answers) => {
      context = {
        ...context,
        metadata: {
          ...(context.metadata || {}),
          ...answers,
        },
      };
      return setProjectConfig(context).then((context) => {
        return inquirer
          .prompt([
            {
              name: "projectSaveBaseConfig",
              message: [t(context.lang, "确认基础配置"), "?"].join(""),
              default: "yes",
              pageSize: 4,
              type: "rawlist",
              choices: [
                ...t(context.lang, boolChoies),
                {
                  name: t(context.lang, "取消"),
                  value: "cancel",
                },
              ],
            },
          ])
          .then((result) => {
            if (result.projectSaveBaseConfig === "yes") {
              log("success", {
                message: t(context.lang, "完成") + t(context.lang, "。"),
              });
              return context;
            } else if (result.projectSaveBaseConfig === "no") {
              log("warn", {
                message: [
                  t(context.lang, "用户取消"),
                  ",",
                  t(context.lang, "重新设置"),
                  t(context.lang, "。"),
                ].join(" "),
              });
              return done(context);
            } else {
              return Promise.reject({
                message: [
                  t(context.lang, "用户取消"),
                  t(context.lang, "。"),
                ].join(""),
              });
            }
          });
      });
    });
  };

  return done(context);
};
